package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.mongo.UserLikeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 右滑  喜欢
     * 1.先判断数据库中 我是否已经喜欢了对方
     * 2.将推荐用户表中数据删除
     * 3.判断双方是否互相喜欢 如果是 返回true(添加mongo的好友关系) 如果不是返回false
     * @param loginUserId
     * @param likeUserId
     * @return
     */
    @Override
    public boolean likeUser(Long loginUserId, Long likeUserId) {
        //1.先判断数据库中 我是否已经喜欢了对方
        Query query1 = new Query(
                Criteria.where("userId").is(loginUserId)
                .and("likeUserId").is(likeUserId)
        );
        UserLike userLike = mongoTemplate.findOne(query1, UserLike.class);
        if(userLike == null){ //往数据库添加一条喜欢动作
            userLike = new UserLike();
            userLike.setUserId(loginUserId);
            userLike.setLikeUserId(likeUserId);
            userLike.setCreated(System.currentTimeMillis());
            mongoTemplate.save(userLike);
        }
        //2.将推荐用户表中数据删除
        Query query2 = new Query(
            Criteria.where("userId").is(likeUserId).and("toUserId").is(loginUserId)
        );
        mongoTemplate.remove(query2 , RecommendUser.class);
        //3.判断双方是否互相喜欢 如果是 返回true(添加mongo的好友关系) 如果不是返回false
        //查询对方是否也喜欢我即可
        Query query3 = new Query(
                Criteria.where("userId").is(likeUserId )
                        .and("likeUserId").is(loginUserId)
        );
        UserLike userLike2 = mongoTemplate.findOne(query3, UserLike.class);
        if(userLike2 == null){ //对方不喜欢我
            return false;
        }else{
            //添加好友关系之前删除关系(删除关系的目的是 保证数据库中只有唯一的关系)
            Query removeQuery1 = new Query(
                    Criteria.where("userId").is(loginUserId).and("friendId").is(likeUserId)
            );
            mongoTemplate.remove(removeQuery1 ,Friend.class );
            Query removeQuery2 = new Query(
                    Criteria.where("userId").is( likeUserId).and("friendId").is(loginUserId )
            );
            mongoTemplate.remove(removeQuery2 ,Friend.class );

            //添加好友关系 我添加对方好友
            Friend friend1 = new Friend();
            friend1.setCreated(System.currentTimeMillis());
            friend1.setUserId( loginUserId );
            friend1.setFriendId( likeUserId );
            mongoTemplate.save(friend1);

            //添加好友关系 对方添加我好友
            Friend friend2 = new Friend();
            friend2.setCreated(System.currentTimeMillis());
            friend2.setUserId( likeUserId );
            friend2.setFriendId(  loginUserId);
            mongoTemplate.save(friend2);

            return true;//对方也喜欢我
        }

    }

    //查询互相喜欢
    @Override
    public Long findEachLoveCount(Long loginUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
        );
        long count = mongoTemplate.count(query, Friend.class);
        return count;
    }

    //查询粉丝
    @Override
    public Long findFanCount(Long loginUserId) {
        Query query = new Query(
                Criteria.where("likeUserId").is(loginUserId)
        );
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    //查询我的喜欢
    @Override
    public Long findLoveCount(Long loginUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
        );
        long count = mongoTemplate.count(query, UserLike.class);
        return count;
    }

    /**
     * 查询 互相喜欢，喜欢，粉丝 访客 数据
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我
     *
     * 整个代码的目的: 将不同的对象 所需要最后的userId  抽取成了一个对象 并且此对象只有userId
     */
    @Override
    public PageBeanVo findUserInfoByType(Integer page, Integer pageSize, String type, Long loginUserId) {
        //直接构建 UserLikeVo 等效 RecommendUserVo
        List<UserLikeVo> userLikeVoList = new ArrayList<>();
        long count = 0L;

        //计算开始索引
        int startIndex = (page-1)*pageSize;
        Query query = null;
        //1.根据type查询四个表
        if(StrUtil.equals(type , "1")){
            //1.1 查询互相关注数据 好友表 friend
            query = new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<Friend> friendList = mongoTemplate.find(query, Friend.class);
            count =  mongoTemplate.count(query, Friend.class);//查询计数
            //数据不为空转换
            if(CollectionUtil.isNotEmpty(friendList)){
                for (Friend friend : friendList) {
                    //转换vo
                    UserLikeVo vo = transferUserLikeVo(friend.getFriendId());
                    userLikeVoList.add(vo);
                }
            }


        }else if(StrUtil.equals(type , "2")){
            //1.2 查询我关注的人 喜欢表 UserLike
             query= new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
            count =  mongoTemplate.count(query, UserLike.class); //查询计数

            //数据不为空转换
            if(CollectionUtil.isNotEmpty(userLikeList)){
                for (UserLike userLike : userLikeList) {
                    //转换vo
                    UserLikeVo vo = transferUserLikeVo(userLike.getLikeUserId());
                    userLikeVoList.add(vo);
                }
            }

        }else if(StrUtil.equals(type , "3")){
            //1.3 查询关注我的人 粉丝 喜欢表 UserLike
            query= new Query(
                    Criteria.where("likeUserId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
            count =  mongoTemplate.count(query, UserLike.class);//查询计数


            //数据不为空转换
            if(CollectionUtil.isNotEmpty(userLikeList)){
                for (UserLike userLike : userLikeList) {
                    //转换vo
                    UserLikeVo vo = transferUserLikeVo(userLike.getUserId());
                    userLikeVoList.add(vo);
                }
            }

        }else if(StrUtil.equals(type , "4")){
            //1.4 查询谁看过我  访客表 Visitor
            query= new Query(
                    Criteria.where("userId").is(loginUserId)
            ).skip(startIndex).limit(pageSize);
            List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
            count =  mongoTemplate.count(query, Visitor.class);//查询计数

            //数据不为空转换
            if(CollectionUtil.isNotEmpty(visitorList)){
                for (Visitor visitor : visitorList) {
                    //转换vo
                    UserLikeVo vo = transferUserLikeVo(visitor.getVisitorUserId());
                    userLikeVoList.add(vo);
                }
            }
        }
        return new PageBeanVo(page , pageSize , count , userLikeVoList);
    }

    //传入的id转换成UserLikeVo
    private UserLikeVo transferUserLikeVo(Long userId){
        UserLikeVo vo = new UserLikeVo();
        vo.setId(userId);
        return vo;
    }

    //喜欢-右滑  添加到usr_like表
    @Override
    public void saveUserLike(Long userId, Long loginUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId).and("likeUserId").is(userId)
        );

        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);

        if (userLike == null) {
            userLike = new UserLike();
            userLike.setCreated(System.currentTimeMillis());
            userLike.setLikeUserId(userId);
            userLike.setUserId(loginUserId);
            mongoTemplate.save(userLike);
        }
        Query query1 = new Query(
                Criteria.where("userId").is(userId).and("toUserId").is(loginUserId)
        );

        mongoTemplate.remove(query1, RecommendUser.class);
    }

    //判断双方是否互相喜欢
    @Override
    public Boolean isLike(Long userId, Long loginUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId).and("likeUserId").is(userId)
        );

        boolean exists = mongoTemplate.exists(query, UserLike.class);

        Query query1 = new Query(
                Criteria.where("userId").is(userId).and("likeUserId").is(loginUserId)
        );
        boolean exists1 = mongoTemplate.exists(query1, UserLike.class);
        boolean b = exists && exists1;
        return b;
    }


    //取消喜欢 删除usr_like表中的数据
    @Override
    public void deleteUserLike(Long userId, Long loginUserId) {
        Query query = new Query(
                Criteria.where("userId").is(loginUserId).and("likeUserId").is(userId)
        );

        mongoTemplate.remove(query, UserLike.class);

        Query query1 = new Query(
                Criteria.where("userId").is(userId).and("toUserId").is(loginUserId)
        );

        mongoTemplate.remove(query1, RecommendUser.class);



    }
}
